﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using System.IO;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// 广州WMS_订单信息创建
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WMSSalesOrderCreateController : ControllerBase
    {
        private readonly ILogger _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;
        public readonly string gz_con;


        public WMSSalesOrderCreateController(ILogger<WMS_SalesOrderCreateRequestDto> logger, IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration, ILoggerFactory loggerFactory)
        {
            _WMSTokenCheckService = wmsTokenCheckService;
            con = _configuration.GetConnectionString("DDM_DbConnection_ZH");
            gz_con = _configuration.GetConnectionString("GZ_WMS_DbConnection");
            _logger = loggerFactory.CreateLogger<WMSSalesOrderCreateController>();

        }

        /// <summary>
        /// 广州WMS_订单信息创建
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostSalesOrderCreate([FromBody] WMS_SalesOrderCreateRequestDto strparm)
        {
            string  strMsg = "";
            string input = "";
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            WMS_SalesOrderCreateResponseDto re = new WMS_SalesOrderCreateResponseDto();

            Dictionary<string, string> dic = new Dictionary<string, string>(); //查询公用dic
            try
            {
                Dictionary<string, string> urldic = new Dictionary<string, string>(); //查询公用dic
                urldic.Add("function_description", "组件WMS_销售订单新增");//查看URL配置表地址描述
                DataTable url_dt = MethodHelp.GetTableInfo("TMS0004", urldic, con);//获取URL地址
                if (url_dt.Rows.Count > 0)
                {

                    string jsonText = JsonConvert.SerializeObject(strparm);
                    var reAgv = DB.httpPost(url_dt.Rows[0]["interface_url"].ToString(),jsonText, "", "否");

                    input = jsonText + "-URL:" + url_dt.Rows[0]["interface_url"].ToString();
                    JObject jsonObject = JObject.Parse(reAgv);
                    if ((string)jsonObject["Response"]["return"]["returnCode"]=="999")
                    {
                        strMsg = (string)jsonObject["Response"]["return"]["resultInfo"].ToString();
                    }                                       
                }
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }

            if (strMsg == "")
            {
                re.code = 0;
                re.message = "";
            }
            else
            {
                re.code = 500;
                re.message = strMsg;

            }



            _logger.LogInformation(JsonConvert.SerializeObject(re) + "->" + input);
            return new JsonResult(re);
        }

      
    }
}
